<template>
  <div class="my-page">
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      height="100%"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="objectId" label="商品编号"> </el-table-column>
      <el-table-column prop="name" label="商品名"> </el-table-column>
      <el-table-column
        prop="chineseBrief"
        width="250"
        label="商品描述"
        align="left"
      >
      </el-table-column>
      <el-table-column prop="img" label="商品图片">
        <template slot-scope="scope">
          <img class="goods-img" :src="scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column>
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="请输入商品名称" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="padding"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="totalNumber"
      @current-change="handlePaginate"
      @prev-click="handlePaginate"
      @next-click="handlePaginate"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "MyProducts",
  mounted() {
    this.getGoodsList();
  },
  data() {
    return {
      search:'',
      tableData: [],
      resData: [],
      multipleSelection: [],
      totalNumber: 0,
      page: 0,
      pageSize: 6,
    };
  },
 watch: {
    search(newVal) {
      if (newVal && newVal.trim().length > 0) {
        this.tableData = this.resData.filter((data) =>
          data.name.toLowerCase().includes(newVal.toLowerCase())
        )
      }else {
        this.getTableList()
      }
    },
  },
  methods: {
    getGoodsList() {
      this.$H.get("/1.1/classes/classify").then((res) => {
        console.log('products',res);
        this.resData = res.data.results;
        this.totalNumber = res.data.results.length;
        this.getTableList();
      });
    },
    //获取table数据
    getTableList() {
      let skip = this.page * this.pageSize;
      let lastSkip = skip + this.pageSize;
      this.tableData = this.resData.slice(skip,lastSkip);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      let {objectId} = row
      this.$router.push(`/products/goodsDetail/${objectId}`)
      this.$store.commit('communication/changePageTextMu','商品详情')

    },
    handleDelete(index, row) {
      let {objectId} = row
      //resData中需要删除的索引
      let idx = this.page * this.pageSize + index
      this.tableData.splice(index,1)
      this.resData.splice(idx,1)
      // this.$H.delete(`/1.1/classes/classify/${objectId}`)
    },
    handlePaginate(index) {
      this.page = index - 1;
      this.getTableList();
    },
  },
};
</script>

<style lang="less" scoped>
.my-page {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;

  .goods-img {
    width: 100%;
    height: 100%;
  }
}
.padding {
  padding: 20px 0;
  text-align: center;
}
/deep/.el-table .cell {
  text-align: center;
}
/deep/.el-tooltip__popper {
  width: 120px;
}
</style>